home *** CD-ROM | disk | FTP | other *** search
/ Aminet 22 / Aminet 22 (1997)(GTI - Schatztruhe)[!][Dec 1997].iso / Aminet / dev / src / ConfigFileSrc.lha / ConfigFileSrc12 / Library / Funcs / Read.c < prev    next >
Encoding:
C/C++ Source or Header  |  1997-10-02  |  12.8 KB  |  559 lines

  1. /*
  2. **        $PROJECT: ConfigFile.library
  3. **        $FILE: Read.c
  4. **        $DESCRIPTION: cf_Read() function
  5. **
  6. **        (C) Copyright 1996-1997 Marcel Karas
  7. **             All Rights Reserved.
  8. */
  9.  
  10. #include "StrConv.h"
  11. #include "WriteBuffer.h"
  12. #include "Read&Write.h"
  13.  
  14. IMPORT struct ExecBase        * SysBase;
  15. IMPORT struct DosLibrary    * DOSBase;
  16.  
  17. ULONG ReadShort ( STRPTR, iCFHeader * );
  18. ULONG ReadASCII ( STRPTR, iCFHeader * );
  19.  
  20. /****** configfile.library/cf_Read *******************************************
  21. *
  22. *   NAME
  23. *        cf_Read -- Read a CF file.
  24. *
  25. *   SYNOPSIS
  26. *        Result = cf_Read(Header,ErrorCode);
  27. *        D0               A0     A1
  28. *
  29. *        BOOL cf_Read(CFHeader *,ULONG *);
  30. *
  31. *   FUNCTION
  32. *        This function clears all nodes and read the CF file new. The
  33. *        CF_HFLG_CHANGED flag in Header->Flags will be clear.
  34. *
  35. *   INPUTS
  36. *        Header - The Header of the file.
  37. *
  38. *        ErrorCode - Contains an errorcode if the function returns FALSE
  39. *                    or NULL.
  40. *
  41. *                CF_RERR_UNKOWN       - Unkown failure.
  42. *                CF_RERR_BUFFER_MEM   - No memory for buffer.
  43. *                CF_RERR_READ_FILE    - Couldn't read the file.
  44. *                CF_RERR_FORMAT       - File has an error in the format
  45. *                                       structure. (V2)
  46. *                CF_RERR_UNKOWN_ITYPE - An unkown item type was found. (V2)
  47. *
  48. *   RESULT
  49. *        Result - TRUE for success or in case of FALSE return, the ErrorCode
  50. *                 var can be read to obtain more.
  51. *
  52. *   SEE ALSO
  53. *        cf_Open(), cf_Close(), cf_Write(), <libraries/configfile.h>
  54. *
  55. ******************************************************************************
  56. *
  57. */
  58.  
  59. LibCall BOOL cf_Read ( REGA0 iCFHeader * Header, REGA1 ULONG * ErrorCode )
  60. {
  61.     APTR    Buffer;
  62.     ULONG    Error, Len = Header->Length;
  63.  
  64.     FuncDe(bug("cf_Read($%08lx,$%08lx)\n{\n", Header, ErrorCode));
  65.  
  66.     if ( Len > 5 )
  67.     {
  68.         Len -= CF_IDENT_LEN;
  69.  
  70.         if ( Buffer = MyAllocPooled (Header->MemPool, Len) )
  71.         {
  72.             Seek (Header->FileHandle, 5, OFFSET_BEGINNING);        
  73.  
  74.             if ( Read (Header->FileHandle, Buffer, Len - 1) != -1 )
  75.             {
  76.                 if ( Header->ExtFlags & CF_EFLG_ALREADY_READ )
  77.                     cf_ClearGrpList(Header);
  78.  
  79.                 Error =    Header->Flags & CF_HFLG_SHORT_FILE ?
  80.                             ReadShort (Buffer, Header) :
  81.                             ReadASCII (Buffer, Header);
  82.  
  83.                 if ( !Error )
  84.                 {
  85.                     MyFreePooled (Header->MemPool, Buffer, Len);
  86.  
  87.                     Header->Flags        &= ~CF_HFLG_CHANGED;
  88.                     Header->ExtFlags    |= CF_EFLG_ALREADY_READ;
  89.  
  90.                     FuncDe(bug("   return(TRUE)\n}\n"));
  91.                     return (TRUE);
  92.                 }
  93.  
  94.                 cf_ClearGrpList(Header);
  95.             }
  96.             else    Error = CF_RERR_READ_FILE;
  97.  
  98.             MyFreePooled (Header->MemPool, Buffer, Len);
  99.         }
  100.         else    Error = CF_RERR_BUFFER_MEM;
  101.     }
  102.     else
  103.     {
  104.         FuncDe(bug("   return(TRUE)\n}\n"));
  105.         return (TRUE);
  106.     }
  107.  
  108.     if ( ErrorCode ) *ErrorCode = Error;
  109.  
  110.     FuncDe(bug("   return(FALSE) Error %ld\n}\n", Error));
  111.     return (FALSE);
  112. }
  113.  
  114. ULONG ReadShort ( STRPTR Buffer, iCFHeader * Header )
  115. {
  116.     iCFGroup        *GrpNode;
  117.     iCFArgument    *ArgNode;
  118.     iCFItem        *ItemNode;
  119.  
  120.     REGISTER STRPTR    Ptr        = Buffer;
  121.     STRPTR                EndAdr    = Buffer + (Header->Length - CF_IDENT_EXTLEN);
  122.     UBYTE                    Type, Ext, StrLen, StructLen;
  123.  
  124. #ifndef _M68020
  125.     UBYTE  TmpArry[sizeof (ULONG)];
  126. #endif
  127.  
  128.     do
  129.     {
  130.         if ( *Ptr )
  131.         {
  132.             StrLen = *Ptr;
  133.             Ptr++;
  134.  
  135.             GrpNode = NewGrp (Header, Ptr, StrLen);
  136.             Ptr += StrLen;
  137.  
  138.             if ( *Ptr == CTRLB_SUB )
  139.             {
  140.                 Ptr++;
  141.  
  142.                 do
  143.                 {
  144.                     if( *Ptr )
  145.                     {
  146.                         StrLen = *Ptr;
  147.                         Ptr++;
  148.  
  149.                         ArgNode = NewArg (GrpNode, Ptr, StrLen);
  150.                         Ptr += StrLen;
  151.  
  152.                         if( *Ptr == CTRLB_SUB )
  153.                         {
  154.                             Ptr++;
  155.  
  156.                             do
  157.                             {
  158.                                 if( *Ptr == CTRLB_END )    { Ptr++; break; }
  159.  
  160.                                 Type    = ( *Ptr & TYP_MASK );
  161.  
  162.                                 if( Type == TYP_STRING )
  163.                                 {
  164.                                     Ptr++;
  165.                                     StructLen    = sizeof (iCFItem) + 2 + *Ptr;
  166.                                     StrLen        = *Ptr;
  167.                                     Ptr++;
  168.  
  169.                                     ItemNode = MyAllocPooled (Header->MemPool, StructLen);
  170.  
  171.                                     ItemNode->SpecialType= 0;
  172.                                     ItemNode->Type            = CF_ITYP_STRING;
  173.                                     ItemNode->ArgNode        = ArgNode;
  174.                                     ItemNode->StructSize    = StructLen;
  175.                                     ItemNode->ExtFlags    = 0;
  176.  
  177.                                     ItemNode->Contents.String    = (STRPTR)
  178.                                         ( (ULONG) ItemNode + sizeof (iCFItem) );
  179.                                     ItemNode->Contents.String[0]            = StrLen;
  180.                                     ItemNode->Contents.String ++;
  181.                                     ItemNode->Contents.String[StrLen]    = 0;
  182.  
  183.                                     MemCpy (ItemNode->Contents.String, Ptr, StrLen);
  184.  
  185.                                     AddTail ((struct List *) &ArgNode->ItemList,
  186.                                         (struct Node *) ItemNode);
  187.  
  188.                                     Ptr += StrLen;
  189.  
  190.                                     continue;
  191.                                 }
  192.     
  193.                                 ItemNode = MyAllocPooled (Header->MemPool, sizeof(iCFItem));
  194.  
  195.                                 Ext    = ( *Ptr & NUM_MASK );
  196.  
  197.                                 ItemNode->SpecialType    = Bin2SType[( *Ptr & STYP_MASK ) >> 3];
  198.                                 ItemNode->ArgNode            = ArgNode;
  199.                                 ItemNode->StructSize        = sizeof(iCFItem);
  200.                                 ItemNode->ExtFlags        = 0;
  201.  
  202.                                 Ptr++;
  203.  
  204.                                 if ( Type == TYP_NUMBER )
  205.                                 {
  206.                                     ItemNode->Type    = CF_ITYP_NUMBER;
  207.  
  208.                                     if ( Ext == NUM_BYTE )
  209.                                     {
  210.                                         ItemNode->Contents.Number    = (ULONG) (*Ptr);
  211.  
  212.                                         Ptr += sizeof(UBYTE);
  213.                                     } 
  214.                                     else if ( Ext == NUM_WORD )
  215.                                     {
  216. #ifdef _M68020
  217.                                         ItemNode->Contents.Number    = (ULONG) *( (UWORD *) Ptr );
  218.                                         Ptr += sizeof (UWORD);
  219. #else
  220.                                         TmpArry[0] = *Ptr++; TmpArry[1] = *Ptr++;
  221.  
  222.                                         ItemNode->Contents.Number    = (ULONG) *( (UWORD *) &TmpArry );
  223. #endif
  224.                                     }
  225.                                     else
  226.                                     {
  227. #ifdef _M68020
  228.                                         ItemNode->Contents.Number    = (ULONG) *( (ULONG *) Ptr );
  229.                                         Ptr += sizeof (ULONG);
  230. #else
  231.                                         TmpArry[0] = *Ptr++; TmpArry[1] = *Ptr++;
  232.                                         TmpArry[2] = *Ptr++; TmpArry[3] = *Ptr++;
  233.  
  234.                                         ItemNode->Contents.Number    = (ULONG) *( (ULONG *) &TmpArry );
  235. #endif
  236.                                     }
  237.                                 }
  238.                                 else if ( Type == TYP_BOOL )
  239.                                 {
  240.                                     ItemNode->Type                    = CF_ITYP_BOOL;
  241.                                     ItemNode->Contents.Bool        = 
  242.                                                     ( Ext == BOOL_TRUE) ? TRUE : FALSE;
  243.                                 }
  244.                                 else
  245.                                 {
  246.                                     MyFreePooled (Header->MemPool, ItemNode, sizeof (iCFItem));
  247.                                     return (CF_RERR_UNKOWN_ITYPE);
  248.                                 }
  249.  
  250.                                 AddTail ((struct List *) &ArgNode->ItemList,
  251.                                                 (struct Node *) ItemNode);
  252.                             }
  253.                             while ( Ptr != EndAdr );
  254.                         }
  255.                     }
  256.                     else
  257.                     {
  258.                         Ptr++;
  259.                         break;
  260.                     }
  261.                 }
  262.                 while ( Ptr != EndAdr );
  263.             }
  264.         }
  265.         else break;
  266.     }
  267.     while ( Ptr != EndAdr );
  268.  
  269.     return (NULL);
  270. }
  271.  
  272. ULONG ReadASCII ( STRPTR Buffer, iCFHeader * Header )
  273. {
  274.     iCFGroup        *GrpNode;
  275.     iCFArgument    *ArgNode;
  276.     iCFItem        *ItemNode;
  277.  
  278.     REGISTER STRPTR    Ptr        = Buffer;
  279.     STRPTR                EndAdr    = Buffer + Header->Length - CF_IDENT_EXTLEN,
  280.                             LinePtr, StartPtr;
  281.     APTR                    MemPool    = Header->MemPool;
  282.     UWORD                    Line        = 2, StrLen;
  283.  
  284. #define    EQUAL            '='
  285. #define    GRP_START    '['
  286. #define    GRP_END        ']'
  287. #define    STR_CHAR        '"'
  288.  
  289. #define    CHAR_QUOTES    0x22
  290.  
  291. #define    CHAR_LINEFEED    '\n'
  292. #define    CHAR_NEXTITEM    ','
  293.  
  294. #define    GetLine()        Line
  295. #define    GetColumn()        Ptr - LinePtr + 1
  296.  
  297. #define    IsNameChar()    ( CType[*Ptr] & CT_NAME_CHARS )
  298. #define    IsStringChar()    ( CType[*Ptr] & CT_STRING_CHARS )
  299. #define    IsSpaceChar()    ( CType[*Ptr] & CT_SPACE_CHARS )
  300. #define    IsBoolChar()    ( CType[*Ptr] & CT_BOOL )
  301.  
  302.     *EndAdr = CHAR_LINEFEED;
  303.  
  304.     LinePtr = Ptr;
  305.  
  306.     while ( Ptr < EndAdr )
  307.     {
  308.         while ( IsSpaceChar() )    Ptr++;
  309.  
  310.         if ( *Ptr == CHAR_LINEFEED )
  311.         {
  312.             Ptr ++;
  313.             while ( *Ptr == CHAR_LINEFEED )
  314.                 { Line ++; Ptr ++; }
  315.             LinePtr = Ptr;
  316.         }
  317.         else if ( *Ptr == GRP_START )
  318.             break;
  319.         else if ( IsNameChar() )
  320.         {
  321.             GrpNode = NewGrp (Header, "", 0);
  322.             if ( !GrpNode ) return (CF_RERR_FORMAT);    // CF_RERR_LEXICAL
  323.             break;
  324.         }
  325.         else
  326.             return (CF_RERR_FORMAT);    // CF_RERR_LEXICAL
  327.     }
  328.  
  329.     while ( Ptr < EndAdr )
  330.     {
  331.         while ( IsSpaceChar() )    Ptr++;
  332.  
  333.         if ( *Ptr == CHAR_LINEFEED )
  334.         {
  335.             Ptr++;
  336.             while ( *Ptr == CHAR_LINEFEED )
  337.                 { Line ++; Ptr ++; }
  338.             LinePtr = Ptr;
  339.         }
  340.         else if ( *Ptr == GRP_START )
  341.         {
  342.             Ptr++;
  343.  
  344.             while ( IsSpaceChar() )    Ptr++;
  345.  
  346.             StartPtr = Ptr;
  347.  
  348.             while ( IsNameChar() )    Ptr++;
  349.  
  350.             StrLen = Ptr - StartPtr;
  351.  
  352.             while ( IsSpaceChar() )    Ptr++;
  353.  
  354.             if ( *Ptr != GRP_END )
  355.                 return (CF_RERR_FORMAT);    // CF_RERR_LEXICAL
  356.  
  357.             Ptr ++;
  358.  
  359.             while ( IsSpaceChar() )    Ptr++;
  360.  
  361.             if ( *Ptr != CHAR_LINEFEED ) 
  362.                 return (CF_RERR_FORMAT);    // CF_RERR_LEXICAL
  363.             
  364.             Ptr ++;
  365.  
  366.             while ( *Ptr == CHAR_LINEFEED )
  367.                 { Line ++; Ptr ++; }
  368.  
  369.             LinePtr = Ptr;
  370.  
  371.             if ( StrLen )
  372.                 GrpNode = NewGrp (Header, StartPtr, StrLen);
  373.             else
  374.                 GrpNode = NewGrp (Header, "", 0);
  375.  
  376.             if ( !GrpNode )
  377.                 return (CF_RERR_FORMAT);    // CF_RERR_LEXICAL
  378.         }
  379.         else if ( IsNameChar() )
  380.         {
  381.             StartPtr = Ptr;
  382.  
  383.             while ( IsNameChar() )    Ptr++;
  384.  
  385.             StrLen = Ptr - StartPtr;
  386.  
  387.             while ( IsSpaceChar() )    Ptr++;
  388.  
  389.             if ( *Ptr == EQUAL )
  390.             {
  391.                 if ( !( ArgNode = NewArg (GrpNode, StartPtr, StrLen) ) )
  392.                     return (CF_RERR_FORMAT);    // CF_RERR_LEXICAL
  393.  
  394.                 Ptr ++;
  395.  
  396.                 while ( Ptr < EndAdr )
  397.                 {
  398.                     while ( IsSpaceChar() )    Ptr++;
  399.  
  400.                     if ( *Ptr == STR_CHAR )            // string item type
  401.                     {
  402.                         UWORD     NodeLen = sizeof (iCFItem) + 2;
  403.  
  404.                         StartPtr = ++Ptr;
  405.                         while ( IsStringChar() ) Ptr++;
  406.  
  407.                         NodeLen += StrLen = Ptr-StartPtr;
  408.  
  409.                         if ( *Ptr != STR_CHAR )            return (CF_RERR_FORMAT);
  410.                         if ( StrLen > CF_MAX_STRLEN )    return (CF_RERR_FORMAT);
  411.  
  412.                         Ptr ++;
  413.  
  414.                         ItemNode = MyAllocPooled (MemPool, NodeLen);
  415.  
  416.                         ItemNode->SpecialType    = 0;
  417.                         ItemNode->Type                = CF_ITYP_STRING;
  418.  
  419.                         ItemNode->ArgNode            = ArgNode;
  420.                         ItemNode->StructSize        = NodeLen;
  421.                         ItemNode->ExtFlags        = 0;
  422.  
  423.                         ItemNode->Contents.String        = (STRPTR) ( (ULONG) ItemNode + sizeof (iCFItem) );
  424.                         ItemNode->Contents.String[0]    = StrLen;
  425.                         ItemNode->Contents.String ++;
  426.                         ItemNode->Contents.String[StrLen]    = 0;
  427.  
  428.                         MemCpy (ItemNode->Contents.String, StartPtr, StrLen);
  429.  
  430.                         AddTail ((struct List *) &ArgNode->ItemList, (struct Node *) ItemNode);
  431.                     }
  432.                     else
  433.                     {
  434.                         if ( !( ItemNode = MyAllocPooled (MemPool, sizeof (iCFItem)) ) )
  435.                             return (CF_RERR_FORMAT);
  436.  
  437.                         ItemNode->ArgNode            = ArgNode;
  438.                         ItemNode->StructSize        = sizeof (iCFItem);
  439.                         ItemNode->ExtFlags        = 0;
  440.  
  441.                         if ( IsBoolChar() )
  442.                         {
  443.                             ItemNode->Type    = CF_ITYP_BOOL;
  444.  
  445.                             if ( *Ptr    == 'T' && Ptr[1] == 'R' &&
  446.                                     Ptr[2] == 'U' && Ptr[3] == 'E' )    // TRUE bool item type
  447.                             {
  448.                                 ItemNode->SpecialType    = CF_STYP_BOOL_TRUE;
  449.                                 ItemNode->Contents.Bool    = TRUE;
  450.                                 Ptr += 4;
  451.                             }
  452.                             else if ( *Ptr   == 'F' && Ptr[1] == 'A' &&
  453.                                          Ptr[2] == 'L' && Ptr[3] == 'S' &&
  454.                                          Ptr[4] == 'E')        // FALSE bool item type
  455.                             {
  456.                                 ItemNode->SpecialType    = CF_STYP_BOOL_TRUE;
  457.                                 ItemNode->Contents.Bool    = FALSE;
  458.                                 Ptr += 5;
  459.                             }
  460.                             else if ( *Ptr   == 'Y' && Ptr[1] == 'E' &&
  461.                                          Ptr[2] == 'S' )        // YES bool item type
  462.                             {
  463.                                 ItemNode->SpecialType    = CF_STYP_BOOL_YES;
  464.                                 ItemNode->Contents.Bool    = TRUE;
  465.                                 Ptr += 3;
  466.                             }
  467.                             else if ( *Ptr == 'N' && Ptr[1] == 'O' )    // NO bool item type
  468.                             {
  469.                                 ItemNode->SpecialType    = CF_STYP_BOOL_YES;
  470.                                 ItemNode->Contents.Bool    = FALSE;
  471.                                 Ptr += 2;
  472.                             }
  473.                             else if ( *Ptr == 'O' )    // ON bool item type
  474.                             {
  475.                                 ItemNode->SpecialType    = CF_STYP_BOOL_ON;
  476.                                 Ptr ++;
  477.  
  478.                                 if ( *Ptr == 'N' )    // OFF bool item type
  479.                                 {
  480.                                     ItemNode->Contents.Bool    = TRUE;
  481.                                     Ptr ++;
  482.                                 }
  483.                                 else if ( *Ptr == 'F' && Ptr[1] == 'F' )    // ON bool item type
  484.                                 {
  485.                                     ItemNode->Contents.Bool    = FALSE;
  486.                                     Ptr += 2;
  487.                                 }
  488.                                 else goto UnkownItem;
  489.                             }
  490.                             else goto UnkownItem;
  491.                         }
  492.                         else if ( ( ( *Ptr >= '0' ) && ( *Ptr <= '9' ) ) ||
  493.                             ( *Ptr == '-' ) )    // DEC number item type
  494.                         {
  495.                             ItemNode->Type                    = CF_ITYP_NUMBER;
  496.                             ItemNode->SpecialType        = CF_STYP_NUM_DEC;
  497.                             Ptr = DecStrToLong (Ptr, &ItemNode->Contents.Number);
  498.                         }
  499.                         else if ( *Ptr == '$' )        // HEX number item type
  500.                         {
  501.                             ItemNode->Type                    = CF_ITYP_NUMBER;
  502.                             ItemNode->SpecialType        = CF_STYP_NUM_HEX;
  503.                             Ptr = HexStrToLong (Ptr, &ItemNode->Contents.Number);
  504.                         }
  505.                         else if ( *Ptr == '%' )        // BIN number item type
  506.                         {
  507.                             ItemNode->Type                    = CF_ITYP_NUMBER;
  508.                             ItemNode->SpecialType        = CF_STYP_NUM_BIN;
  509.                             Ptr = BinStrToLong (Ptr, &ItemNode->Contents.Number);
  510.                         }
  511.                         else
  512.                         {
  513. UnkownItem:
  514.                             MyFreePooled (MemPool, ItemNode, sizeof (iCFItem));
  515.                             return (CF_RERR_UNKOWN_ITYPE);
  516.                         }
  517.  
  518.                         AddTail ((struct List *) &ArgNode->ItemList, (struct Node *) ItemNode);
  519.                     }
  520.  
  521.                     while ( IsSpaceChar() )    Ptr++;
  522.  
  523.                     if ( *Ptr == CHAR_LINEFEED )
  524.                     {
  525.                         Ptr++;
  526.                         while ( *Ptr == CHAR_LINEFEED )
  527.                             { Line ++; Ptr ++; }
  528.                         LinePtr = Ptr;
  529.                         break;
  530.                     }
  531.                     else if ( *Ptr == CHAR_NEXTITEM )
  532.                         Ptr ++;
  533.                     else return (CF_RERR_FORMAT);
  534.                 }
  535.             }
  536.             else if ( *Ptr == CHAR_LINEFEED )
  537.             {
  538.                 if ( !( ArgNode = NewArg (GrpNode, StartPtr, StrLen) ) )
  539.                     return (CF_RERR_FORMAT);    // CF_RERR_LEXICAL
  540.  
  541.                 Ptr ++;
  542.  
  543.                 while ( *Ptr == CHAR_LINEFEED )
  544.                     { Line ++; Ptr ++; }
  545.  
  546.                 LinePtr = Ptr;
  547.             }
  548.             else
  549.                 return (CF_RERR_FORMAT);    // CF_RERR_LEXICAL
  550.         }
  551.         else
  552.         {
  553.             return (CF_RERR_FORMAT);    // CF_RERR_LEXICAL
  554.         }
  555.     }
  556.  
  557.     return(NULL);
  558. }
  559.